import 'package:flutter/material.dart';

class SecuritizationScreen extends StatefulWidget {
  const SecuritizationScreen({super.key});

  @override
  State<SecuritizationScreen> createState() => _SecuritizationScreenState();
}

class _SecuritizationScreenState extends State<SecuritizationScreen> {
  String? selectedAsset;
  final TextEditingController _amountController = TextEditingController();
  final List<Map<String, dynamic>> securitizationProducts = [
    {
      'name': '文化资产证券化基金',
      'type': 'REITs',
      'minInvestment': 1000,
      'expectedReturn': '6.8%',
      'duration': '3年',
      'risk': '中低风险',
      'description': '投资优质文化资产，享受稳定收益',
      'color': const Color(0xFF1890FF),
      'assets': ['苏绣作品集', '古琴收藏', '木雕艺术品'],
    },
    {
      'name': '非遗传承证券化',
      'type': 'ABS',
      'minInvestment': 5000,
      'expectedReturn': '8.5%',
      'duration': '5年',
      'risk': '中风险',
      'description': '支持非遗传承，获得长期回报',
      'color': const Color(0xFF52C41A),
      'assets': ['传统工艺', '文化IP', '技艺传承'],
    },
    {
      'name': '数字文化资产池',
      'type': 'Token Fund',
      'minInvestment': 500,
      'expectedReturn': '12.3%',
      'duration': '1年',
      'risk': '中高风险',
      'description': '数字化文化资产投资组合',
      'color': const Color(0xFFFA8C16),
      'assets': ['数字藏品', 'NFT作品', '虚拟展览'],
    },
  ];

  final List<Map<String, dynamic>> myAssets = [
    {
      'name': '明代青花瓷',
      'type': '古董文物',
      'value': 150000,
      'status': '已认证',
      'icon': Icons.local_florist,
      'color': const Color(0xFF1890FF),
    },
    {
      'name': '苏绣屏风',
      'type': '传统工艺',
      'value': 80000,
      'status': '评估中',
      'icon': Icons.palette,
      'color': const Color(0xFF52C41A),
    },
    {
      'name': '古琴「流水」',
      'type': '乐器收藏',
      'value': 200000,
      'status': '已认证',
      'icon': Icons.music_note,
      'color': const Color(0xFFFA8C16),
    },
    {
      'name': '木雕观音',
      'type': '雕刻艺术',
      'value': 120000,
      'status': '已认证',
      'icon': Icons.carpenter,
      'color': const Color(0xFF722ED1),
    },
  ];

  @override
  void dispose() {
    _amountController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        title: const Text(
          '资产证券化',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        backgroundColor: const Color(0xFF1890FF),
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () => Navigator.pop(context),
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.help_outline, color: Colors.white),
            onPressed: () => _showHelpDialog(),
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildHeaderCard(),
            const SizedBox(height: 20),
            _buildProductsSection(),
            const SizedBox(height: 20),
            _buildMyAssetsSection(),
            const SizedBox(height: 20),
            _buildInvestmentForm(),
            const SizedBox(height: 20),
            _buildMyInvestmentsSection(),
          ],
        ),
      ),
    );
  }

  Widget _buildHeaderCard() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        gradient: const LinearGradient(
          colors: [Color(0xFF1890FF), Color(0xFF40A9FF)],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: const Color(0xFF1890FF).withOpacity(0.3),
            blurRadius: 10,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.2),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Icon(
                  Icons.account_balance,
                  color: Colors.white,
                  size: 24,
                ),
              ),
              const SizedBox(width: 12),
              const Expanded(
                child: Text(
                  '资产证券化服务',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 16),
          const Text(
            '将文化资产转化为可交易证券\n提供流动性，实现资产价值最大化',
            style: TextStyle(
              color: Colors.white,
              fontSize: 14,
              height: 1.5,
            ),
          ),
          const SizedBox(height: 16),
          Row(
            children: [
              _buildStatItem('资产总值', '¥5,500,000'),
              const SizedBox(width: 20),
              _buildStatItem('已证券化', '¥2,800,000'),
              const SizedBox(width: 20),
              _buildStatItem('累计收益', '¥380,000'),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildStatItem(String label, String value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            color: Colors.white.withOpacity(0.8),
            fontSize: 12,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          value,
          style: const TextStyle(
            color: Colors.white,
            fontSize: 14,
            fontWeight: FontWeight.bold,
          ),
        ),
      ],
    );
  }

  Widget _buildProductsSection() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '证券化产品',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
            color: Color(0xFF333333),
          ),
        ),
        const SizedBox(height: 12),
        ...securitizationProducts.map((product) => _buildProductCard(product)),
      ],
    );
  }

  Widget _buildProductCard(Map<String, dynamic> product) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(
          color: product['color'].withOpacity(0.2),
          width: 1,
        ),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: product['color'].withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(
                  Icons.account_balance_wallet,
                  color: product['color'],
                  size: 20,
                ),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      product['name'],
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: Color(0xFF333333),
                      ),
                    ),
                    Text(
                      '${product['type']} • ${product['description']}',
                      style: const TextStyle(
                        fontSize: 12,
                        color: Color(0xFF666666),
                      ),
                    ),
                  ],
                ),
              ),
              Text(
                product['expectedReturn'],
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: product['color'],
                ),
              ),
            ],
          ),
          const SizedBox(height: 12),
          Row(
            children: [
              _buildProductInfo('期限', product['duration']),
              const SizedBox(width: 20),
              _buildProductInfo('起投', '${product['minInvestment']} USDT'),
              const SizedBox(width: 20),
              _buildProductInfo('风险', product['risk']),
            ],
          ),
          const SizedBox(height: 12),
          Wrap(
            spacing: 8,
            children: (product['assets'] as List<String>).map((asset) {
              return Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: product['color'].withOpacity(0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  asset,
                  style: TextStyle(
                    fontSize: 10,
                    color: product['color'],
                    fontWeight: FontWeight.w500,
                  ),
                ),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }

  Widget _buildProductInfo(String label, String value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: const TextStyle(
            fontSize: 10,
            color: Color(0xFF999999),
          ),
        ),
        Text(
          value,
          style: const TextStyle(
            fontSize: 12,
            fontWeight: FontWeight.w500,
            color: Color(0xFF333333),
          ),
        ),
      ],
    );
  }

  Widget _buildMyAssetsSection() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Text(
                '我的资产',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: Color(0xFF333333),
                ),
              ),
              TextButton(
                onPressed: () {
                  // 添加新资产
                },
                child: const Text(
                  '+ 添加资产',
                  style: TextStyle(
                    fontSize: 14,
                    color: Color(0xFF1890FF),
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 12),
          ...myAssets.map((asset) => _buildAssetItem(asset)),
        ],
      ),
    );
  }

  Widget _buildAssetItem(Map<String, dynamic> asset) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF8F9FA),
        borderRadius: BorderRadius.circular(8),
        border: Border.all(
          color: asset['color'].withOpacity(0.2),
          width: 1,
        ),
      ),
      child: Row(
        children: [
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: asset['color'].withOpacity(0.1),
              borderRadius: BorderRadius.circular(20),
            ),
            child: Icon(
              asset['icon'],
              color: asset['color'],
              size: 20,
            ),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  asset['name'],
                  style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    color: Color(0xFF333333),
                  ),
                ),
                Text(
                  '${asset['type']} • ¥${asset['value'].toString().replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]},')}',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF666666),
                  ),
                ),
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
            decoration: BoxDecoration(
              color: asset['status'] == '已认证' 
                  ? const Color(0xFF52C41A).withOpacity(0.1)
                  : const Color(0xFFFA8C16).withOpacity(0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Text(
              asset['status'],
              style: TextStyle(
                fontSize: 10,
                color: asset['status'] == '已认证' 
                    ? const Color(0xFF52C41A)
                    : const Color(0xFFFA8C16),
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          const SizedBox(width: 8),
          IconButton(
            onPressed: () => _showAssetSecuritizationDialog(asset),
            icon: const Icon(
              Icons.monetization_on,
              color: Color(0xFF1890FF),
              size: 20,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildInvestmentForm() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '投资证券化产品',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: Color(0xFF333333),
            ),
          ),
          const SizedBox(height: 16),
          const Text(
            '选择产品',
            style: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: Color(0xFF333333),
            ),
          ),
          const SizedBox(height: 8),
          _buildProductSelector(),
          const SizedBox(height: 16),
          const Text(
            '投资金额',
            style: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: Color(0xFF333333),
            ),
          ),
          const SizedBox(height: 8),
          _buildAmountInput(),
          const SizedBox(height: 20),
          _buildInvestButton(),
        ],
      ),
    );
  }

  Widget _buildProductSelector() {
    return Container(
      height: 120,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: securitizationProducts.length,
        itemBuilder: (context, index) {
          final product = securitizationProducts[index];
          final isSelected = selectedAsset == product['name'];
          return GestureDetector(
            onTap: () {
              setState(() {
                selectedAsset = product['name'];
              });
            },
            child: Container(
              width: 160,
              margin: const EdgeInsets.only(right: 12),
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: isSelected ? product['color'].withOpacity(0.1) : const Color(0xFFF8F9FA),
                borderRadius: BorderRadius.circular(12),
                border: Border.all(
                  color: isSelected ? product['color'] : Colors.transparent,
                  width: 2,
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    product['name'],
                    style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.bold,
                      color: isSelected ? product['color'] : const Color(0xFF333333),
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 4),
                  Text(
                    product['type'],
                    style: const TextStyle(
                      fontSize: 10,
                      color: Color(0xFF666666),
                    ),
                  ),
                  const Spacer(),
                  Text(
                    product['expectedReturn'],
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: isSelected ? product['color'] : const Color(0xFF333333),
                    ),
                  ),
                  Text(
                    '预期年化',
                    style: const TextStyle(
                      fontSize: 10,
                      color: Color(0xFF999999),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  Widget _buildAmountInput() {
    return Column(
      children: [
        TextField(
          controller: _amountController,
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
            hintText: '请输入投资金额',
            suffixText: 'USDT',
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
            ),
            focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: const BorderSide(color: Color(0xFF1890FF)),
            ),
          ),
        ),
        const SizedBox(height: 8),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            const Text(
              '可用余额: 10,000 USDT',
              style: TextStyle(
                fontSize: 12,
                color: Color(0xFF666666),
              ),
            ),
            Row(
              children: [
                _buildQuickAmountButton('1000'),
                const SizedBox(width: 8),
                _buildQuickAmountButton('5000'),
                const SizedBox(width: 8),
                _buildQuickAmountButton('全部'),
              ],
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildQuickAmountButton(String amount) {
    return GestureDetector(
      onTap: () {
        if (amount == '全部') {
          _amountController.text = '10000';
        } else {
          _amountController.text = amount;
        }
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
        decoration: BoxDecoration(
          color: const Color(0xFF1890FF).withOpacity(0.1),
          borderRadius: BorderRadius.circular(4),
        ),
        child: Text(
          amount,
          style: const TextStyle(
            fontSize: 12,
            color: Color(0xFF1890FF),
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  Widget _buildInvestButton() {
    return SizedBox(
      width: double.infinity,
      height: 48,
      child: ElevatedButton(
        onPressed: selectedAsset != null && _amountController.text.isNotEmpty
            ? () => _confirmInvestment()
            : null,
        style: ElevatedButton.styleFrom(
          backgroundColor: const Color(0xFF1890FF),
          foregroundColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
          ),
          elevation: 0,
        ),
        child: const Text(
          '确认投资',
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

  Widget _buildMyInvestmentsSection() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Text(
                '我的投资',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: Color(0xFF333333),
                ),
              ),
              TextButton(
                onPressed: () {
                  // 查看全部投资记录
                },
                child: const Text(
                  '查看全部',
                  style: TextStyle(
                    fontSize: 14,
                    color: Color(0xFF1890FF),
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 12),
          _buildInvestmentRecord(),
          _buildInvestmentRecord(),
        ],
      ),
    );
  }

  Widget _buildInvestmentRecord() {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF8F9FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        children: [
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: const Color(0xFF1890FF).withOpacity(0.1),
              borderRadius: BorderRadius.circular(20),
            ),
            child: const Icon(
              Icons.account_balance_wallet,
              color: Color(0xFF1890FF),
              size: 20,
            ),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  '文化资产证券化基金',
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    color: Color(0xFF333333),
                  ),
                ),
                const Text(
                  '5000 USDT • 6.8% • 还剩2年3个月',
                  style: TextStyle(
                    fontSize: 12,
                    color: Color(0xFF666666),
                  ),
                ),
              ],
            ),
          ),
          const Text(
            '+340 USDT',
            style: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.bold,
              color: Color(0xFF52C41A),
            ),
          ),
        ],
      ),
    );
  }

  void _showAssetSecuritizationDialog(Map<String, dynamic> asset) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('资产证券化'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('资产名称: ${asset['name']}'),
              Text('资产类型: ${asset['type']}'),
              Text('评估价值: ¥${asset['value'].toString().replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]},')}'),
              const SizedBox(height: 16),
              const Text(
                '证券化方案:',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
              const SizedBox(height: 8),
              const Text('• 将资产打包成证券化产品'),
              const Text('• 发行资产支持证券(ABS)'),
              const Text('• 提供流动性和投资机会'),
              const Text('• 预期年化收益: 7.5%'),
              const SizedBox(height: 16),
              Container(
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: const Color(0xFFFFF7E6),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Text(
                  '证券化需要专业评估和监管审批，周期约3-6个月。',
                  style: TextStyle(
                    fontSize: 12,
                    color: Color(0xFFFA8C16),
                  ),
                ),
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('取消'),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context);
                _processAssetSecuritization(asset);
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: const Color(0xFF1890FF),
                foregroundColor: Colors.white,
              ),
              child: const Text('申请证券化'),
            ),
          ],
        );
      },
    );
  }

  void _confirmInvestment() {
    if (selectedAsset == null || _amountController.text.isEmpty) return;

    final selectedProduct = securitizationProducts.firstWhere(
      (product) => product['name'] == selectedAsset,
    );

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('确认投资'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('产品: $selectedAsset'),
              Text('投资金额: ${_amountController.text} USDT'),
              Text('预期收益率: ${selectedProduct['expectedReturn']}'),
              Text('投资期限: ${selectedProduct['duration']}'),
              Text('风险等级: ${selectedProduct['risk']}'),
              const SizedBox(height: 16),
              Container(
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: const Color(0xFFFFF7E6),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Text(
                  '投资有风险，收益不保证。请仔细阅读产品说明书。',
                  style: TextStyle(
                    fontSize: 12,
                    color: Color(0xFFFA8C16),
                  ),
                ),
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('取消'),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context);
                _processInvestment();
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: const Color(0xFF1890FF),
                foregroundColor: Colors.white,
              ),
              child: const Text('确认投资'),
            ),
          ],
        );
      },
    );
  }

  void _processInvestment() {
    // 模拟投资处理
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return const AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              CircularProgressIndicator(),
              SizedBox(height: 16),
              Text('正在处理投资申请...'),
            ],
          ),
        );
      },
    );

    // 模拟网络延迟
    Future.delayed(const Duration(seconds: 2), () {
      Navigator.pop(context); // 关闭加载对话框
      
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('投资成功'),
            content: const Text('您的投资申请已成功提交，资金已划转至托管账户。'),
            actions: [
              ElevatedButton(
                onPressed: () {
                  Navigator.pop(context);
                  setState(() {
                    _amountController.clear();
                    selectedAsset = null;
                  });
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xFF1890FF),
                  foregroundColor: Colors.white,
                ),
                child: const Text('确定'),
              ),
            ],
          );
        },
      );
    });
  }

  void _processAssetSecuritization(Map<String, dynamic> asset) {
    // 模拟资产证券化处理
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return const AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              CircularProgressIndicator(),
              SizedBox(height: 16),
              Text('正在提交证券化申请...'),
            ],
          ),
        );
      },
    );

    // 模拟网络延迟
    Future.delayed(const Duration(seconds: 2), () {
      Navigator.pop(context); // 关闭加载对话框
      
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('申请已提交'),
            content: Text('您的${asset['name']}证券化申请已提交，我们将在3-5个工作日内完成初步评估。'),
            actions: [
              ElevatedButton(
                onPressed: () => Navigator.pop(context),
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xFF1890FF),
                  foregroundColor: Colors.white,
                ),
                child: const Text('确定'),
              ),
            ],
          );
        },
      );
    });
  }

  void _showHelpDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('资产证券化说明'),
          content: const SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  '什么是资产证券化？',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),
                SizedBox(height: 8),
                Text(
                  '资产证券化是将缺乏流动性的资产转换为可在金融市场上交易的证券的过程。',
                  style: TextStyle(fontSize: 14),
                ),
                SizedBox(height: 16),
                Text(
                  '如何参与？',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),
                SizedBox(height: 8),
                Text(
                  '1. 资产持有者：将文化资产申请证券化，获得流动性\n2. 投资者：购买证券化产品，获得投资收益',
                  style: TextStyle(fontSize: 14),
                ),
                SizedBox(height: 16),
                Text(
                  '风险提示',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                    color: Color(0xFFFA8C16),
                  ),
                ),
                SizedBox(height: 8),
                Text(
                  '• 投资有风险，收益不保证\n• 证券化产品可能面临流动性风险\n• 文化资产价值评估存在主观性\n• 请根据自身风险承受能力投资',
                  style: TextStyle(
                    fontSize: 14,
                    color: Color(0xFFFA8C16),
                  ),
                ),
              ],
            ),
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('我知道了'),
            ),
          ],
        );
      },
    );
  }
}